// 组件打包公共配置
const { base, senior, options } = require('./public');
const { merge } = require('webpack-merge');
const path = require('path');
const {port} = require('../config/index')

const entry = {}
if (process.env.components === 'web' || !process.env.components) {
  Object.assign(entry, {
    web: {
      import: path.resolve(__dirname, '../src/core/entry/web.ts'),
      library: {
        name: '__inbiz_components_web',
        type: 'var',
      },
    },
    webpreview: {
      import: path.resolve(__dirname, '../src/core/entry/webPreview.ts'),
      library: {
        name: '__inbiz_components_preview',
        type: 'var',
      },
    }
  })
}
if (process.env.components === 'wap' || !process.env.components) {
  Object.assign(entry, {
    wap: {
      import: path.resolve(__dirname, '../src/core/entry/wap.ts'),
      library: {
        name: '__inbiz_components_wap',
        type: 'var',
      },
    },
    wappreview: {
      import: path.resolve(__dirname, '../src/core/entry/wapPreview.ts'),
      library: {
        name: '__inbiz_components_preview',
        type: 'var',
      },
    }
  })
}

if (options.isBuild) {
  Object.assign(entry, {
    locale: {
      import: path.resolve(__dirname, '../src/core/entry/locales/index.ts'),
      library: {
        type: 'commonjs',
        export: 'default'
      }
    }
  })
}
module.exports = merge(base, {
  entry,
  output: {
    filename: '[name].js',
    path: path.resolve(__dirname, '../dist/components'),
    chunkLoadingGlobal: `components_${Date.now()}_${Math.random()
      .toString()
      .slice(2)}`,
  },
  devServer: {
    port: port.component,
  },
  externals: senior,
  resolve: {
    alias: {
      '@components': path.resolve(__dirname, '../src/core/components'),
      '@common': path.resolve(__dirname, '../src/core/common'),
      '@assets': path.resolve(__dirname, '../src/assets'),
    },
  },
});
